<?php 
//echo "<pre>";print_r($this->models);exit;
$db = &JFactory::getDbo();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Windsor Reward</title>
<link type="text/css" href="<?php echo JURI::root();?>/css/buttons.css" rel="stylesheet" />
<script src="http://code.jquery.com/jquery-1.9.0.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.0.0.js"></script>
<style>
    body { font-family: Arial,Helvetica,sans-serif; font-size: 80%; }
    .print-redemption { border:1px solid #CCC; border-width:1px 0 0 1px; }
    .print-redemption-label, .print-redemption-value { border:1px solid #CCC; border-width:0 1px 1px 0; padding: 5px 8px; }
    .print-redemption-label { font-weight: bold;  }
    .product-redemption-label, .product-redemption-value { border:1px solid #CCC; border-width:0 1px 1px 0; padding: 4px 8px; text-align: left; }
     
</style>
<script>
	jQuery(document).ready(function(){
		jQuery('.viewdetailredemption').click(function(){
			var this_id = jQuery(this).attr('id');
			//jQuery('.poutlethidden').hide();
			jQuery('.'+this_id).toggle();
			
		});
	});
</script>
</head>

<body>
<?php 
	
	if(!empty($this->models) && !empty($this->user)):
	
?>
<div style="width: 850px; margin: 20px auto 0;">
    <table style="float: right;">
    	<tbody>
    		<tr>
    			<td></td>
    			<td><b>WMC VIP</b></td>
    		</tr>
    		<tr>
    			<td></td>
    			<td>193 Tran Hung Dao, District 1 Vietnam</td>
    		</tr>
    		<tr>
    			<td>Phone:</td>
    			<td>3920 6515 ext 6070</td>
    		</tr>
    		<tr>
    			<td>Email:</td>
    			<td>info@wmcvip.vn</td>
    		</tr>
    		<tr>
    			<td>Website:</td>
    			<td>www.wmcvip.vn</td>
    		</tr>
    	</tbody>
    </table>
    <br clear="all">
    <br>
    <div style="float: left">
    	<b>Purchase Order No.</b>	<?php echo $this->models[0]->id; ?>
    </div>
    <div style="text-align: right;">
        
		<!--<a class="ui-state-default ui-corner-all special-link" id="update_transaction" href="JavaScript:window.print()" style="cursor: pointer; font-size: .9em; font-weight: normal; padding-right: 20px;"><span class="ui-icon ui-icon-newwin"></span>Print</a>-->
		<a class="ui-state-default ui-corner-all special-link" id="update_transaction" href="<?php echo JURI::root() ?>administrator/index.php?option=com_wrd&controller=profile&layout=exredemption&code=<?php echo strval($_GET['code']);?>" style="cursor: pointer; font-size: .9em; font-weight: normal; padding-right: 20px;"><span class="ui-icon ui-icon-newwin"></span>Export exel</a>
        <a class="ui-state-default ui-corner-all special-link" id="update_transaction" onclick="window.location=&quot;index.php?option=com_wrd&controller=profile&id=<?php echo $this->user->id; ?>&layout=form&tab=redemption&quot;" style="cursor: pointer; font-size: .9em; font-weight: normal; padding-right: 20px;"><span class="ui-icon ui-icon-newwin"></span>Cancel</a>
    </div>
    <br><br>
    <table class="print-redemption" cellpadding="0" cellspacing="0" style="width: 100%;">
        <tbody>
            <tr valign="top">
                <td class="print-redemption-label" style="width: 20%;">Member Name:</td>
                <td width="35.8%" class="print-redemption-value"><strong><?php echo $this->user->l_name.' '.$this->user->f_name; ?></strong></td>
				<td class="print-redemption-label">Outlet pick up:</td>
                <td class="print-redemption-value"><?php echo $this->models[0]->outlet; ?></td>
			</tr>
            <tr valign="top">
                <td class="print-redemption-label" style="width: 20%;">Member No:</td>
                <td width="35.8%" class="print-redemption-value"><?php echo ($this->user->member_id); ?></td>
				<td class="print-redemption-label">Requested Outlet:</td>
                <td class="print-redemption-value"><?php echo $this->models[0]->requested_outlet_name; ?></td>
			</tr>
            <tr valign="top">
                <td class="print-redemption-label" style="width: 20%;">Phone No:</td>
                <td width="35.8%" class="print-redemption-value"><?php echo ($this->user->phone); ?></td>
				<td class="print-redemption-label">Created date</td>
                <td class="print-redemption-value"><?php echo strtotime($this->models[0]->exchange_date) ? date( 'd-m-Y',strtotime($this->models[0]->exchange_date)) : ''; ?></td>
			</tr>
             <tr valign="top">
                <td class="print-redemption-label" style="width: 20%;">Address:</td>
                <td class="print-redemption-value"><?php echo ($this->user->address); ?></td>
				<td class="print-redemption-label">Pick Up Date:</td>
                <td class="print-redemption-value"><?php echo strtotime($this->models[0]->pick_up_date)? date( 'd-m-Y', strtotime($this->models[0]->pick_up_date)) : ''; ?></td>
			</tr>
			<tr>
                <td class="print-redemption-label">Redemption Code:</td>
                <td class="print-redemption-value"><?php echo $this->models[0]->code; ?></td>
				<td class="print-redemption-label">Status:</td>
                <td class="print-redemption-value">
                    <?php 

						if($this->models[0]->approved == 1) : 
							echo 'Approve';
						elseif($this->models[0]->approved == 0) :
							echo 'Waiting';
						elseif($this->models[0]->approved == 2) :
							echo 'UnApproved';
						elseif($this->models[0]->approved == 9) :
							echo 'Deleted by us';	
						endif;
                    ?>
                </td>
			</tr>
			
            
            <tr valign="top">
                <td class="print-redemption-label">Products:</td>
                <td colspan="3">
                    <table cellspacing="0" cellpadding="0" style="width: 100%;">
                        <thead>
                            <tr class="active" style="display: table-row;background: #e6e6e5">
                            	<!--<th style="text-align: center; font-size: 10px;" class="product-redemption-label">Product Code</th>-->
                                <th style="text-align: center; font-size: 10px;" class="product-redemption-label">Product Name</th>
                                <th style="text-align: center; font-size: 10px;" class="product-redemption-label">Quantity</th>
								<th style="text-align: center; font-size: 10px;" class="product-redemption-label">Voucher Serial No.</th>
								<th style="text-align: center; font-size: 10px;" class="product-redemption-label">Expired</th>
								<th style="text-align: center; font-size: 10px;" class="product-redemption-label">Unit Price</th>
                                <th style="text-align: center; font-size: 10px;" class="product-redemption-label">Type Redemption</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php 
                                $totalPoint = 0;
                                $totalCash = 0;
                               // echo '<pre>';print_r($this->models);
								foreach($this->models as $item):
									$product_arr = unserialize($item->product);
									//print_r($item->product);
									$product = $product_arr['en-GB'];
									//print_r($item);
								   //<td class="product-redemption-label" style="width: 13%;font-size: 10px;">'.$item->p_code.'</td>
								   echo '
                                        <tr>
                                        	
                                            <td class="product-redemption-label" style="width: 25%;font-size: 10px;">'.$product.'</td>
                                            <td class="product-redemption-label" style="text-align: center; width:10%;font-size: 10px;">'.$item->quantity.'</td>
											<td class="product-redemption-label" style="text-align: center;width:25%;font-size: 10px;">'.$item->code_voucher.'</td>
											';
											echo '<td class="product-redemption-label" style="width: 11%; font-size: 10px;">';
													
													echo !strtotime($item->expired) ? '0000-00-00' : date("d-m-Y",strtotime($item->expired)); 

											echo '</td>
											<td class="product-redemption-label" style="width: 12%;font-size: 10px;">'.$item->price.' VND</td>
											</td>
                                            <td class="product-redemption-label" style="font-size: 10px;">
                                                '.($item->cash > 0 ? 'Point: '.$item->point.'. Cash: '.number_format($item->cash, 2, '.', ',') : 'Point: '.$item->point).'
                                            </td>
											';
											
                                        echo '</tr>
                                    ';
                                    $totalPoint += $item->point;
                                    $totalCash += $item->cash;
                                endforeach;
                            ?>
                        </tbody>
                    </table>
                </td>
            </tr>
			
			<tr valign="top">
                <td class="print-redemption-label">Transaction:</td>
                <td colspan="3">
                    <table cellspacing="0" cellpadding="0" style="width: 100%;">
                        <thead>
                            <tr class="active" style="display: table-row;background: #e6e6e5">
                                <th style="text-align: center;" class="product-redemption-label">ID</th>
                                <th style="text-align: center;" class="product-redemption-label">Outlet</th>
                                <th style="text-align: center;" class="product-redemption-label">Used Point</th>
								<th style="text-align: center;" class="product-redemption-label">Percentage</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php 
								//echo "<pre>";print_r($this->tr);
								$tmp_tr = array();
								//if($this->admin->groups[8] == 8){
									//prepair content
									foreach ($this->tr as $key => $row) {
										$tmp_tr[$row->poulet][data][] = $row;
										$tmp_tr[$row->poulet][total] = abs($row->point) + $tmp_tr[$row->poulet][total];
										$tmp_tr[$row->poulet][outlet] = $row->name;
										$tmp_tr[$row->poulet][outletid] = $row->poulet;
									}
									?>
									<?php $total = 1; foreach($tmp_tr as $k=>$row):  ?>
									<tr style="background: #f2f2f2;">
										<td class="product-redemption-label" style="text-align: center">
											<span class="viewdetailredemption" 
											id="outlet_<?php echo $row[outletid]; ?>" 
											style="cursor: pointer">
											<small>View Detail</small></span>
										</td>
										<td class="product-redemption-label">
											<?php echo $row[outlet]; ?>
										</td>
										<td class="product-redemption-label">
											<?php echo $row[total]; ?>
										</td>
										<td class="product-redemption-label">
											<?php echo number_format(($row[total]/$totalPoint)*100,2, '.', ',') ?>%
										</td>
									</tr>
										<?php foreach($row[data] as $data_line):  
											
											
											?>
											<tr class="poutlethidden outlet_<?php echo $data_line->poulet; ?>" style="display: none; ">
												<td class="product-redemption-label" style="text-align: right">
													<?php echo $data_line->transaction; ?></td>
												<td class="product-redemption-label">
													
													<?php 
														
														$sql = "SELECT des,MIN(id),type FROM #__wrd_point WHERE transaction=".$data_line->transaction;
														
														$db->setQuery($sql);
														$des = $db->loadObject();
														if($des->type == 1)
															echo $des->des;
														elseif($des->type == 2)
															echo $des->des != 'Point Adjustment' ? 'Point Adjustment - '.$des->des: '';
														elseif($des->type == 0)
															echo $row[outlet];
														else
															echo 'Contact Web Team';
														
													?>
												</td>
												<td class="product-redemption-label">
													<?php echo abs($data_line->point); ?></td>
												<td class="product-redemption-label"></td>
											</tr>
										<?php endforeach; ?>
									<?php endforeach; ?>
									<?php
								//}
								//else{
									// foreach($this->tr as $item):
	                                    // echo '
	                                        // <tr>
	                                            // <td class="product-redemption-label" style="width: 16%;">'.$item->transaction.'</td>
	                                            // <td class="product-redemption-label" style="width:42%;">';
													// if($item->name != ''){
														// echo $item->name;
													// } 
													// else if(($item->type == 9) && ($item->transaction == 0)){
														// echo "WMCRewards - Start Point";
													// }
													// else if(($item->type == 0) &&($item->transaction == 0)){
														// echo "WMCRewards";
													// }
													// else { 
														// $db = JFactory::getDbo();
															// if(empty($item->name)){
																// $sql = "SELECT des,MIN(id) FROM #__wrd_point WHERE transaction=".$item->transaction;
																// $db->setQuery($sql);
																// $des = $db->loadObjectList();
																// echo "WMC GROUP ".$des[0]->des != '' ? " - ".$des[0]->des: '';
															// }
														// //echo $des[0]->des;
													// }
												// echo '</td>
	                                            // <td class="product-redemption-label">
	                                                // '.substr($item->point,1).'
	                                            // </td>
												// <td class="product-redemption-label">
	                                                // '.number_format((substr($item->point,1)/$totalPoint)*100,2, '.', ',').'%
	                                            // </td>
	                                        // </tr>
	                                    // ';
	                                // endforeach;
                                //}
                            ?>
                        </tbody>
                    </table>
                </td>
            </tr>
            
			<tr>
                <td class="print-redemption-label" style="">Total:</td>
                <td colspan="3" class="print-redemption-value" style="font-weight: bold;">
                    <?php  
                        echo ($totalPoint > 0 ? 'Point: '.$totalPoint : '');
                        echo ($totalCash > 0 ? ' Cash: '.number_format($totalCash, 2, '.', ',') : '');
                    ?>
                </td>
            </tr>            
        </tbody>
    </table>
    <br />
    <!--<table width="100%" >
    	<tr>
    		<td width="25%" align="left" style="font-size: 10px;">Prepared by<br>(MS/Admin)</td>
    		<td width="25%" align="left" style="font-size: 10px;">Checked by<br>(Loyalty Program Manager/ Director)</td>
    		<td width="25%" align="center" style="font-size: 10px;">Verified by<br>(Accounting)</td>
    		<td width="25%" align="center" style="font-size: 10px;">Approved by<br>(Mr. Tony)</td>
    	</tr>
    </table>-->
	<br /><br /><br />
</div>
</body>
</html>
<?php 
    endif;
    exit();
?>